<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Oauth
 *
 * @property int $id
 * @property int $user_id 用户id
 * @property string $platform 第三方平台
 * @property string $type 应用类型
 * @property string|null $unionid 第三方唯一id
 * @property string $openid 第三方openid
 * @property string $openname 第三方昵称
 * @property string|null $access_token AccessToken
 * @property string|null $refresh_token RefreshToken
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth query()
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth whereAccessToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth whereOpenid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth whereOpenname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth wherePlatform($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth whereRefreshToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth whereUnionid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Oauth whereUserId($value)
 * @mixin \Eloquent
 */
class Oauth extends Model
{
    use HasDateTimeFormatter;

    protected $table = 'oauth';

    /**
     * @param int $user_id
     * @param string $openid
     * @param string $nickname
     * @return bool
     */
    public static function addData(int $user_id,string $openid,string $nickname): bool
    {
        $model = new self;
        $model->user_id = $user_id;
        $model->platform = '微信小程序';
        $model->type = 'wx_app';
        $model->openid = $openid;
        $model->openname = $nickname;
        return $model->save();
    }

    /**
     * @param string $type
     * @param string $openid
     * @param string $platform
     * @return mixed
     */
    public static function findUser(string $type, string $openid, string $platform = '微信小程序'){
        return self::query()->where('type',$type)->where('openid',$openid)->where('platform',$platform)->first();
    }

    /**
     * @param string $type
     * @param string $user_id
     * @param string $platform
     * @return mixed
     */
    public static function findUserId(string $type, string $user_id, string $platform = '微信小程序'){
        return self::query()->where('type',$type)->where('user_id',$user_id)->where('platform',$platform)->first();
    }

}
